/**
 * 
 */
package com.demo2do.strawberry.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Secure;
import com.demo2do.strawberry.entity.startup.Startup;
import com.demo2do.strawberry.entity.startup.fund.FundProgress;
import com.demo2do.strawberry.entity.startup.fund.FundProgressType;
import com.demo2do.strawberry.entity.user.profile.Profile;
import com.demo2do.strawberry.service.StartupService;

/**
 * @author lute
 *
 */
@Controller
@RequestMapping("/startups")
@MenuSetting("startups")
public class StartupFundController {
	
	@Autowired
	private StartupService startupService;
	
	/**
	 * prepare to create fund progress
	 * 
	 * @param id
	 * @param fundStatus
	 * @return
	 */
	@RequestMapping("/{id}/fund-progress/create")
	public ModelAndView createFundProgress(@PathVariable("id") Long id) {
		
		ModelAndView modelAndView = new ModelAndView("startup/fund/fund-progress-create");
		
		Startup startup = startupService.load(id);
		modelAndView.addObject("startup", startup);
		modelAndView.addObject("fundProgressType", FundProgressType.aliasOf(startup.getFundStatus()));
		
		return modelAndView;
	}
	
	/**
	 * @param id
	 * @param fundStatus
	 * @param profile
	 * @param fundProgress
	 * @return
	 */
	@RequestMapping(value = "/{id}/fund-progress", method = RequestMethod.POST)
	public String onCreateFundProgress(@PathVariable("id") Long id, @Secure(property = "profile") Profile profile, FundProgress fundProgress) {
		
		Startup startup = startupService.load(id);
		
		startupService.createFundProgress(fundProgress, profile, startup);
		
		return "redirect:/startups/" + id;
	}
	
}
